{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Part 3: Serving\n",
    "\n",
    "In this part you use MLRun's **serving runtime** to deploy your trained models from the previous stage, a `Voting Ensemble` using **max vote** logic.\n",
    "You also use MLRun's **Feature store** to receive the latest tag of the online **Feature Vector** you defined in the previous stage.\n",
    "\n",
    "By the end of this tutorial you’ll learn how to:\n",
    "- Define a model class to load your models, run preprocessing, and predict on the data\n",
    "- Define a Voting Ensemble function on top of your models\n",
    "- Test the serving function locally using your `mock server`\n",
    "- Deploy the function to the cluster and test it live"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Environment setup\n",
    "\n",
    "First, make sure SciKit-Learn is installed in the correct version:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "jupyter": {
     "outputs_hidden": false
    },
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Requirement already satisfied: scikit-learn in /conda/envs/mlrun-extended/lib/python3.9/site-packages (1.2.1)\n",
      "Requirement already satisfied: numpy>=1.17.3 in /conda/envs/mlrun-extended/lib/python3.9/site-packages (from scikit-learn) (1.22.4)\n",
      "Requirement already satisfied: joblib>=1.1.1 in /conda/envs/mlrun-extended/lib/python3.9/site-packages (from scikit-learn) (1.2.0)\n",
      "Requirement already satisfied: threadpoolctl>=2.0.0 in /conda/envs/mlrun-extended/lib/python3.9/site-packages (from scikit-learn) (3.1.0)\n",
      "Requirement already satisfied: scipy>=1.3.2 in /conda/envs/mlrun-extended/lib/python3.9/site-packages (from scikit-learn) (1.10.0)\n"
     ]
    }
   ],
   "source": [
    "!pip install -U scikit-learn"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "Restart your kernel post installing.\n",
    "Since your work is done in this project scope, you should define the project itself for all your MLRun work in this notebook."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "project_name = \"fraud-demo\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "> 2023-02-15 14:48:31,777 [info] loaded project fraud-demo from MLRun DB\n"
     ]
    }
   ],
   "source": [
    "import mlrun\n",
    "\n",
    "# Initialize the MLRun project object\n",
    "project = mlrun.get_or_create_project(project_name, context=\"./\", user_project=True)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Define model class\n",
    "- Load models\n",
    "- Predict from the feature store online service via the `source` key"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "# mlrun: start-code"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "from cloudpickle import load\n",
    "from mlrun.serving.v2_serving import V2ModelServer\n",
    "\n",
    "\n",
    "class ClassifierModel(V2ModelServer):\n",
    "    def load(self):\n",
    "        \"\"\"load and initialize the model and/or other elements\"\"\"\n",
    "        model_file, extra_data = self.get_model(\".pkl\")\n",
    "        self.model = load(open(model_file, \"rb\"))\n",
    "\n",
    "    def predict(self, body: dict) -> list:\n",
    "        \"\"\"Generate model predictions from sample\"\"\"\n",
    "        print(f\"Input -> {body['inputs']}\")\n",
    "        feats = np.asarray(body[\"inputs\"])\n",
    "        result: np.ndarray = self.model.predict(feats)\n",
    "        return result.tolist()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "# mlrun: end-code"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Define a serving function\n",
    "\n",
    "MLRun serving can produce managed real-time serverless pipelines from various tasks, including MLRun models or standard model files.\n",
    "The pipelines use the Nuclio real-time serverless engine, which can be deployed anywhere.\n",
    "[Nuclio](https://docs.nuclio.io/) is a high-performance open-source serverless framework that's focused on data, I/O, and compute-intensive workloads.\n",
    "\n",
    "The **EnrichmentVotingEnsemble** and the **EnrichmentModelRouter** router classes auto-enrich the request with data from the feature store.\n",
    "The router input accepts a list of inference requests (each request can be a dict or list of incoming features/keys). It enriches the request with data from the specified feature vector (`feature_vector_uri`).\n",
    "\n",
    "In many cases the features can have null values (None, NaN, Inf, ..). The `Enrichment` routers can substitute the null \n",
    "value with fixed or statistical value per feature. This is done through the `impute_policy` parameter, which accepts the \n",
    "impute policy per feature (where `*` is used to specify the default). The value can be a fixed number for constants or \n",
    "`$mean`, `$max`, `$min`, `$std`, `$count` for statistical values, to substitute the value with the equivalent feature stats (taken from the feature store).  \n",
    "\n",
    "The following code achieves:\n",
    "\n",
    "- Gather ClassifierModel code from this notebook\n",
    "- Define `EnrichmentVotingEnsemble` - Max-Vote based ensemble with feature enrichment and imputing\n",
    "- Add the previously trained models to the ensemble"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/svg+xml": [
       "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n",
       "<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\"\n",
       " \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\n",
       "<!-- Generated by graphviz version 2.43.0 (0)\n",
       " -->\n",
       "<!-- Title: mlrun&#45;flow Pages: 1 -->\n",
       "<svg width=\"800pt\" height=\"196pt\"\n",
       " viewBox=\"0.00 0.00 799.73 196.00\" xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\">\n",
       "<g id=\"graph0\" class=\"graph\" transform=\"scale(1 1) rotate(0) translate(4 192)\">\n",
       "<title>mlrun&#45;flow</title>\n",
       "<polygon fill=\"white\" stroke=\"transparent\" points=\"-4,4 -4,-192 795.73,-192 795.73,4 -4,4\"/>\n",
       "<!-- _start -->\n",
       "<g id=\"node1\" class=\"node\">\n",
       "<title>_start</title>\n",
       "<polygon fill=\"lightgrey\" stroke=\"black\" points=\"364.89,-152.05 367.04,-152.15 369.17,-152.3 371.26,-152.49 373.32,-152.74 375.33,-153.03 377.29,-153.36 379.18,-153.75 381,-154.18 382.74,-154.65 384.4,-155.16 385.98,-155.71 387.45,-156.31 388.83,-156.94 390.1,-157.61 391.27,-158.31 392.33,-159.04 393.28,-159.8 394.11,-160.59 394.83,-161.41 395.43,-162.25 395.92,-163.11 396.29,-163.99 396.55,-164.89 396.7,-165.8 396.74,-166.72 396.67,-167.65 396.5,-168.59 396.23,-169.53 395.87,-170.47 395.41,-171.41 394.86,-172.35 394.23,-173.28 393.52,-174.2 392.74,-175.11 391.89,-176.01 390.97,-176.89 389.99,-177.75 388.96,-178.59 387.87,-179.41 386.74,-180.2 385.57,-180.96 384.36,-181.69 383.12,-182.39 381.84,-183.06 380.54,-183.69 379.22,-184.29 377.87,-184.84 376.51,-185.35 375.13,-185.82 373.74,-186.25 372.34,-186.64 370.93,-186.97 369.51,-187.26 368.09,-187.51 366.66,-187.7 365.23,-187.85 363.79,-187.95 362.36,-188 360.92,-188 359.49,-187.95 358.05,-187.85 356.62,-187.7 355.19,-187.51 353.77,-187.26 352.35,-186.97 350.94,-186.64 349.54,-186.25 348.15,-185.82 346.77,-185.35 345.41,-184.84 344.06,-184.29 342.74,-183.69 341.44,-183.06 340.16,-182.39 338.92,-181.69 337.71,-180.96 336.54,-180.2 335.41,-179.41 334.32,-178.59 333.29,-177.75 332.31,-176.89 331.39,-176.01 330.54,-175.11 329.76,-174.2 329.05,-173.28 328.42,-172.35 327.87,-171.41 327.41,-170.47 327.05,-169.53 326.78,-168.59 326.61,-167.65 326.54,-166.72 326.58,-165.8 326.73,-164.89 326.99,-163.99 327.36,-163.11 327.85,-162.25 328.46,-161.41 329.17,-160.59 330.01,-159.8 330.95,-159.04 332.01,-158.31 333.18,-157.61 334.45,-156.94 335.83,-156.31 337.31,-155.71 338.88,-155.16 340.54,-154.65 342.28,-154.18 344.1,-153.75 345.99,-153.36 347.95,-153.03 349.96,-152.74 352.02,-152.49 354.11,-152.3 356.24,-152.15 358.39,-152.05 360.56,-152 362.72,-152 364.89,-152.05\"/>\n",
       "<text text-anchor=\"middle\" x=\"361.64\" y=\"-166.3\" font-family=\"Times,serif\" font-size=\"14.00\">start</text>\n",
       "</g>\n",
       "<g id=\"node2\" class=\"node\">\n",
       "<title></title>\n",
       "<polygon fill=\"none\" stroke=\"black\" points=\"388.64,-86.54 388.64,-101.46 372.82,-112 350.46,-112 334.64,-101.46 334.64,-86.54 350.46,-76 372.82,-76 388.64,-86.54\"/>\n",
       "<polygon fill=\"none\" stroke=\"black\" points=\"392.64,-84.4 392.64,-103.6 374.04,-116 349.25,-116 330.64,-103.6 330.64,-84.4 349.25,-72 374.04,-72 392.64,-84.4\"/>\n",
       "</g>\n",
       "<!-- _start&#45;&gt; -->\n",
       "<g id=\"edge1\" class=\"edge\">\n",
       "<title>_start&#45;&gt;</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M361.64,-151.84C361.64,-144.16 361.64,-134.88 361.64,-126.05\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"365.14,-126.03 361.64,-116.03 358.14,-126.03 365.14,-126.03\"/>\n",
       "</g>\n",
       "<!-- transaction_fraud_rf -->\n",
       "<g id=\"node3\" class=\"node\">\n",
       "<title>transaction_fraud_rf</title>\n",
       "<ellipse fill=\"none\" stroke=\"black\" cx=\"104.64\" cy=\"-18\" rx=\"104.78\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"104.64\" y=\"-14.3\" font-family=\"Times,serif\" font-size=\"14.00\">transaction_fraud_rf</text>\n",
       "</g>\n",
       "<!-- &#45;&gt;transaction_fraud_rf -->\n",
       "<g id=\"edge2\" class=\"edge\">\n",
       "<title>&#45;&gt;transaction_fraud_rf</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M330.9,-84.15C290.22,-72.43 217.59,-51.52 165.59,-36.55\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"166.39,-33.14 155.81,-33.73 164.45,-39.86 166.39,-33.14\"/>\n",
       "</g>\n",
       "<!-- transaction_fraud_xgboost -->\n",
       "<g id=\"node4\" class=\"node\">\n",
       "<title>transaction_fraud_xgboost</title>\n",
       "<ellipse fill=\"none\" stroke=\"black\" cx=\"361.64\" cy=\"-18\" rx=\"133.78\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"361.64\" y=\"-14.3\" font-family=\"Times,serif\" font-size=\"14.00\">transaction_fraud_xgboost</text>\n",
       "</g>\n",
       "<!-- &#45;&gt;transaction_fraud_xgboost -->\n",
       "<g id=\"edge3\" class=\"edge\">\n",
       "<title>&#45;&gt;transaction_fraud_xgboost</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M361.64,-71.99C361.64,-64.06 361.64,-54.91 361.64,-46.48\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"365.14,-46.31 361.64,-36.31 358.14,-46.31 365.14,-46.31\"/>\n",
       "</g>\n",
       "<!-- transaction_fraud_adaboost -->\n",
       "<g id=\"node5\" class=\"node\">\n",
       "<title>transaction_fraud_adaboost</title>\n",
       "<ellipse fill=\"none\" stroke=\"black\" cx=\"652.64\" cy=\"-18\" rx=\"139.18\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"652.64\" y=\"-14.3\" font-family=\"Times,serif\" font-size=\"14.00\">transaction_fraud_adaboost</text>\n",
       "</g>\n",
       "<!-- &#45;&gt;transaction_fraud_adaboost -->\n",
       "<g id=\"edge4\" class=\"edge\">\n",
       "<title>&#45;&gt;transaction_fraud_adaboost</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M392.78,-85.08C437.78,-73.64 522.21,-52.17 582.62,-36.81\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"583.65,-40.16 592.48,-34.3 581.92,-33.37 583.65,-40.16\"/>\n",
       "</g>\n",
       "</g>\n",
       "</svg>\n"
      ],
      "text/plain": [
       "<graphviz.graphs.Digraph at 0x7f208d6484c0>"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Create the serving function from your code above\n",
    "serving_fn = project.set_function(\n",
    "    name=\"transaction-fraud\", kind=\"serving\", image=\"mlrun/mlrun\"\n",
    ").apply(mlrun.auto_mount())\n",
    "\n",
    "serving_fn.set_topology(\n",
    "    \"router\",\n",
    "    \"mlrun.serving.routers.EnrichmentVotingEnsemble\",\n",
    "    name=\"VotingEnsemble\",\n",
    "    feature_vector_uri=\"transactions-fraud-short\",\n",
    "    impute_policy={\"*\": \"$mean\"},\n",
    ")\n",
    "\n",
    "model_names = [\n",
    "    \"transaction_fraud_rf\",\n",
    "    \"transaction_fraud_xgboost\",\n",
    "    \"transaction_fraud_adaboost\",\n",
    "]\n",
    "\n",
    "for i, name in enumerate(model_names, start=1):\n",
    "    serving_fn.add_model(\n",
    "        name,\n",
    "        class_name=\"ClassifierModel\",\n",
    "        model_path=project.get_artifact_uri(f\"{name}#{i}:latest\"),\n",
    "    )\n",
    "\n",
    "# Plot the ensemble configuration\n",
    "serving_fn.spec.graph.plot()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Test the server locally\n",
    "\n",
    "Before deploying the serving function, you can test it in the current notebook and check the model output."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "> 2023-02-15 14:48:36,438 [info] model transaction_fraud_rf was loaded\n",
      "> 2023-02-15 14:48:36,482 [info] model transaction_fraud_xgboost was loaded\n",
      "> 2023-02-15 14:48:36,520 [info] model transaction_fraud_adaboost was loaded\n"
     ]
    }
   ],
   "source": [
    "# Create a mock server from the serving function\n",
    "local_server = serving_fn.to_mock_server()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Input -> [[60.98, 73.78999999999999, 2.0, 36.894999999999996, 134.16, 1037.48, 32.0, 32.42125, 143.87, 1861.8400000000001, 59.0, 31.556610169491528, 90.0, 1.0, 2.0]]\n",
      "Input -> [[60.98, 73.78999999999999, 2.0, 36.894999999999996, 134.16, 1037.48, 32.0, 32.42125, 143.87, 1861.8400000000001, 59.0, 31.556610169491528, 90.0, 1.0, 2.0]]Input -> [[60.98, 73.78999999999999, 2.0, 36.894999999999996, 134.16, 1037.48, 32.0, 32.42125, 143.87, 1861.8400000000001, 59.0, 31.556610169491528, 90.0, 1.0, 2.0]]\n",
      "\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "X does not have valid feature names, but RandomForestClassifier was fitted with feature names\n",
      "X does not have valid feature names, but AdaBoostClassifier was fitted with feature names\n",
      "X does not have valid feature names, but GradientBoostingClassifier was fitted with feature names\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "{'id': '5237524f362a47b78828d9d7f7f87d9a',\n",
       " 'model_name': 'VotingEnsemble',\n",
       " 'outputs': [0],\n",
       " 'model_version': 'v1'}"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Choose an id for your test\n",
    "sample_id = \"C1000148617\"\n",
    "\n",
    "model_inference_path = \"/v2/models/infer\"\n",
    "\n",
    "# Send your sample ID for prediction\n",
    "local_server.test(path=model_inference_path, body={\"inputs\": [[sample_id]]})\n",
    "\n",
    "# notice the input vector is printed 3 times (once per child model) and is enriched with data from the feature store"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Accessing the real-time feature vector directly\n",
    "\n",
    "You can also directly query the feature store values using the `get_online_feature_service` method. This method is used internally in the EnrichmentVotingEnsemble router class."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[{'amount_max_2h': 60.98,\n",
       "  'amount_max_12h': 134.16,\n",
       "  'amount_max_24h': 143.87,\n",
       "  'amount_sum_2h': 73.78999999999999,\n",
       "  'amount_sum_12h': 1037.48,\n",
       "  'amount_sum_24h': 1861.8400000000001,\n",
       "  'amount_count_2h': 2.0,\n",
       "  'amount_count_12h': 32.0,\n",
       "  'amount_count_24h': 59.0,\n",
       "  'es_transportation_sum_14d': 90.0,\n",
       "  'es_health_sum_14d': 1.0,\n",
       "  'es_otherservices_sum_14d': 2.0,\n",
       "  'amount_avg_2h': 36.894999999999996,\n",
       "  'amount_avg_12h': 32.42125,\n",
       "  'amount_avg_24h': 31.556610169491528}]"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import mlrun.feature_store as fstore\n",
    "\n",
    "# Create the online feature service\n",
    "svc = \"transactions-fraud-short:latest\".get_online_feature_service(\n",
    "    impute_policy={\"*\": \"$mean\"}\n",
    ")\n",
    "\n",
    "# Get sample feature vector\n",
    "sample_fv = svc.get([{\"source\": sample_id}])\n",
    "sample_fv"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Deploying the function on the Kubernetes cluster\n",
    "\n",
    "You can now deploy the function. Once deployed, you get a function with http trigger that can be called from other locations.\n",
    "\n",
    "Model activities can be tracked into a real-time stream and time-series DB. The monitoring data\n",
    "is used to create real-time dashboards, detect drift, and analyze performance.<br>\n",
    "To monitor a deployed model, apply `set_tracking()`.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Enable model monitoring\n",
    "serving_fn.set_tracking()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from mlrun.datastore.datastore_profile import DatastoreProfileV3io\n",
    "\n",
    "tsdb_profile = DatastoreProfileV3io(name=\"v3io-tsdb-profile\")\n",
    "project.register_datastore_profile(tsdb_profile)\n",
    "\n",
    "stream_profile = DatastoreProfileV3io(\n",
    "    name=\"v3io-stream-profile\",\n",
    "    v3io_access_key=mlrun.mlconf.get_v3io_access_key(),\n",
    ")\n",
    "project.register_datastore_profile(stream_profile)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "project.set_model_monitoring_credentials(\n",
    "    tsdb_profile_name=tsdb_profile.name,\n",
    "    stream_profile_name=stream_profile.name,\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "> 2023-02-15 14:48:36,931 [info] Starting remote function deploy\n",
      "2023-02-15 14:48:39  (info) Deploying function\n",
      "2023-02-15 14:48:39  (info) Building\n",
      "2023-02-15 14:48:39  (info) Staging files and preparing base images\n",
      "2023-02-15 14:48:39  (info) Building processor image\n",
      "2023-02-15 14:50:15  (info) Build complete\n",
      "2023-02-15 14:51:05  (info) Function deploy complete\n",
      "> 2023-02-15 14:51:05,648 [info] successfully deployed function: {'internal_invocation_urls': ['nuclio-fraud-demo-dani-transaction-fraud.default-tenant.svc.cluster.local:8080'], 'external_invocation_urls': ['fraud-demo-dani-transaction-fraud-fraud-demo-dani.default-tenant.app.vmdev94.lab.iguazeng.com/']}\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "'http://fraud-demo-dani-transaction-fraud-fraud-demo-dani.default-tenant.app.vmdev94.lab.iguazeng.com/'"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Deploy the serving function\n",
    "serving_fn.deploy()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Test the server\n",
    "\n",
    "You can test the serving function and examine the model output."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "> 2023-02-15 14:51:05,714 [info] invoking function: {'method': 'POST', 'path': 'http://nuclio-fraud-demo-dani-transaction-fraud.default-tenant.svc.cluster.local:8080/v2/models/infer'}\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "{'id': 'c34706e4-f1c8-4aff-b226-020c2cad7e4a',\n",
       " 'model_name': 'VotingEnsemble',\n",
       " 'outputs': [0],\n",
       " 'model_version': 'v1'}"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Choose an id for your test\n",
    "sample_id = \"C1000148617\"\n",
    "\n",
    "model_inference_path = \"/v2/models/infer\"\n",
    "\n",
    "# Send your sample ID for prediction\n",
    "serving_fn.invoke(path=model_inference_path, body={\"inputs\": [[sample_id]]})"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "You can also directly query the feature store values, which are used in the enrichment."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Simulate incoming data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Load the dataset\n",
    "data = mlrun.get_dataitem(\n",
    "    \"https://s3.wasabisys.com/iguazio/data/fraud-demo-mlrun-fs-docs/data.csv\"\n",
    ").as_df()\n",
    "\n",
    "# use only first 10k\n",
    "data = data.sort_values(by=\"source\", axis=0)[:10000]\n",
    "\n",
    "# keys\n",
    "sample_ids = data[\"source\"].to_list()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "> 2023-02-15 14:51:47,845 [info] invoking function: {'method': 'POST', 'path': 'http://nuclio-fraud-demo-dani-transaction-fraud.default-tenant.svc.cluster.local:8080/v2/models/infer'}\n",
      "{'id': 'f09841c5-4427-4ea1-95a9-723bb09349bb', 'model_name': 'VotingEnsemble', 'outputs': [0], 'model_version': 'v1'}\n",
      "> 2023-02-15 14:51:49,373 [info] invoking function: {'method': 'POST', 'path': 'http://nuclio-fraud-demo-dani-transaction-fraud.default-tenant.svc.cluster.local:8080/v2/models/infer'}\n",
      "{'id': 'd8dd6ca2-d448-4953-aa84-1414f6274f91', 'model_name': 'VotingEnsemble', 'outputs': [0], 'model_version': 'v1'}\n",
      "> 2023-02-15 14:51:49,725 [info] invoking function: {'method': 'POST', 'path': 'http://nuclio-fraud-demo-dani-transaction-fraud.default-tenant.svc.cluster.local:8080/v2/models/infer'}\n",
      "{'id': '8aa2c1cb-5fdf-49e7-9b30-15c4b606bbe2', 'model_name': 'VotingEnsemble', 'outputs': [0], 'model_version': 'v1'}\n",
      "> 2023-02-15 14:51:50,581 [info] invoking function: {'method': 'POST', 'path': 'http://nuclio-fraud-demo-dani-transaction-fraud.default-tenant.svc.cluster.local:8080/v2/models/infer'}\n",
      "{'id': '4357ee2a-c0ca-476d-a04c-add47487391a', 'model_name': 'VotingEnsemble', 'outputs': [0], 'model_version': 'v1'}\n",
      "> 2023-02-15 14:51:51,542 [info] invoking function: {'method': 'POST', 'path': 'http://nuclio-fraud-demo-dani-transaction-fraud.default-tenant.svc.cluster.local:8080/v2/models/infer'}\n",
      "{'id': '324c5938-82b5-4a68-b61b-204530e4b8c9', 'model_name': 'VotingEnsemble', 'outputs': [0], 'model_version': 'v1'}\n",
      "> 2023-02-15 14:51:52,476 [info] invoking function: {'method': 'POST', 'path': 'http://nuclio-fraud-demo-dani-transaction-fraud.default-tenant.svc.cluster.local:8080/v2/models/infer'}\n",
      "{'id': '523c8e5c-ab91-4c8b-83d1-3d57cfa7a5cd', 'model_name': 'VotingEnsemble', 'outputs': [0], 'model_version': 'v1'}\n",
      "> 2023-02-15 14:51:53,067 [info] invoking function: {'method': 'POST', 'path': 'http://nuclio-fraud-demo-dani-transaction-fraud.default-tenant.svc.cluster.local:8080/v2/models/infer'}\n",
      "{'id': '3a03000a-9223-4304-948b-66b3651a38de', 'model_name': 'VotingEnsemble', 'outputs': [0], 'model_version': 'v1'}\n",
      "> 2023-02-15 14:51:53,662 [info] invoking function: {'method': 'POST', 'path': 'http://nuclio-fraud-demo-dani-transaction-fraud.default-tenant.svc.cluster.local:8080/v2/models/infer'}\n",
      "{'id': 'b65943ac-ffbe-4ab9-b209-36611ca2c6cb', 'model_name': 'VotingEnsemble', 'outputs': [0], 'model_version': 'v1'}\n",
      "> 2023-02-15 14:51:54,543 [info] invoking function: {'method': 'POST', 'path': 'http://nuclio-fraud-demo-dani-transaction-fraud.default-tenant.svc.cluster.local:8080/v2/models/infer'}\n",
      "{'id': '85791d18-e959-46e6-ae5f-cdc901c2dce3', 'model_name': 'VotingEnsemble', 'outputs': [0], 'model_version': 'v1'}\n",
      "> 2023-02-15 14:51:54,972 [info] invoking function: {'method': 'POST', 'path': 'http://nuclio-fraud-demo-dani-transaction-fraud.default-tenant.svc.cluster.local:8080/v2/models/infer'}\n",
      "{'id': '73d49f78-0f0d-4a4f-a905-61d4fed44cba', 'model_name': 'VotingEnsemble', 'outputs': [0], 'model_version': 'v1'}\n"
     ]
    }
   ],
   "source": [
    "from random import choice, uniform\n",
    "from time import sleep\n",
    "\n",
    "# Sending random requests\n",
    "for _ in range(10):\n",
    "    data_point = choice(sample_ids)\n",
    "    try:\n",
    "        resp = serving_fn.invoke(\n",
    "            path=model_inference_path, body={\"inputs\": [[data_point]]}\n",
    "        )\n",
    "        print(resp)\n",
    "        sleep(uniform(0.2, 1.7))\n",
    "    except OSError:\n",
    "        pass"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "## Done!\n",
    "\n",
    "You've completed Part 3 of the deploying the serving function.\n",
    "Proceed to [Part 4](04-pipeline.ipynb) to learn how to automate ML Pipeline.\n",
    "\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
